#include "def.h"
#include <set>
#include "guard.h"
#include "log.h"
#include <map>

extern vector<realorder> criticalvr;
extern cmutex criticalvrlock_;
extern int traded_share_number;
extern int MAXTRADEDNUM;
extern set<string> liquidatedstocks;

////////////////////////////////////////
void cdiff::updatespread_market(bool ismaster, TickType t, const spread& firstitemofspreads){
  /*cguard gd(lock_);
    if (traded_share_number < MAXTRADEDNUM){
  //if (t==BID || t==ASK){}
  long_spread_diff = master.ask - slave.bid;
  short_spread_diff =master.bid - slave.ask;
  TTPrint("[%s]long_spread_diff:%f short_spread_diff:%f\n",__FUNCTION__, long_spread_diff, short_spread_diff);

  if(master.ask!=0.0 && slave.bid!=0.0 && master.bid!=0.0 && slave.ask!=0.0){
  if (firstitemofspreads.liq_lower<long_spread_diff && long_spread_diff<firstitemofspreads.liq_upper && 
  firstitemofspreads.liq_lower< short_spread_diff && short_spread_diff < firstitemofspreads.liq_upper)
  {
  if(currentposition!=0){
  TTPrint("[liquidate]position to clear = %d\n",currentposition);
  int masterposition = ismaster? currentposition : -currentposition;
  if (!criticalvr.empty()){
  criticalvr.clear();
  }
  liquidate_marketorder(master.symbol,slave.symbol,masterposition);
  liquidatedstocks.insert(master.symbol);//TODO
  }
  }else if(firstitemofspreads.short_spread_threshold < short_spread_diff ){
  tradespread_marketorder(master.symbol,slave.symbol,firstitemofspreads.unit,false);
  }else if(long_spread_diff < firstitemofspreads.long_spread_threshold){
  tradespread_marketorder(master.symbol,slave.symbol,firstitemofspreads.unit,true);
  }else{
  //TTPrint("no action\n");
  }
  }
  }*/
  //TODO
}

extern bool DISABLETRADE;
extern double MOVEPERTRADE;

//const double tradenum2ONE_UNIT_EVERY_OPPS_TRADE[10]={50,50,100};

// To prevent trade multiple times upon duplicated signal
#define TIMEGAPBETWEENTRADES 60*5
void cdiff::u_limit(bool ismaster, TickType t, spread* band){
  cguard gd(criticalvrlock_);
  if(master.ask!=0.0 && slave.bid!=0.0 && master.bid!=0.0 && slave.ask!=0.0){
    lspread = master.ask - slave.bid;
    sspread = master.bid - slave.ask;
    
    //double spreadsum = lspread - sspread;
    double spreadsum = 0;
    if (traded_share_number < MAXTRADEDNUM && (band->unit>0)){
      //if (t==BID || t==ASK){}
      if (band->liq_lower<lspread && lspread<band->liq_upper && band->liq_lower< sspread && sspread < band->liq_upper){
        if(masterpos!=0 || slavepos!=0){
          TTPrint("[ACTION]liquidatespread.\n");
          if(!DISABLETRADE && (time(NULL)-band->lasttradetime)>TIMEGAPBETWEENTRADES){
            band->lasttradetime = time(NULL);
            liquidate_limitorder(mc,master.bid,master.ask,sc,slave.bid,slave.ask,masterpos,slavepos);
          }
        }
      }else if(band->s_threshold + spreadsum < sspread ){
        TTPrint("[ACTION]shortspread(%f~%f),-%s(%f)+%s(%f).\n",lspread,sspread,mc->symbol.c_str(),master.bid,sc->symbol.c_str(),slave.ask);
        if(!DISABLETRADE && (time(NULL)-band->lasttradetime)>TIMEGAPBETWEENTRADES){
          band->lasttradetime = time(NULL);
          tradespread_limitorder(mc,master.bid,sc,slave.ask,band->unit,false);
          band->s_threshold += MOVEPERTRADE;////
        }
      }else if(lspread + spreadsum < band->l_threshold){
        TTPrint("[ACTION]longspread(%f~%f),+%s(%f)-%s(%f).\n",lspread,sspread,mc->symbol.c_str(),master.ask,sc->symbol.c_str(),slave.bid);
        if(!DISABLETRADE && (time(NULL)-band->lasttradetime)>TIMEGAPBETWEENTRADES){
          band->lasttradetime = time(NULL);
          tradespread_limitorder(mc,master.ask,sc,slave.bid,band->unit,true);
          band->l_threshold -= MOVEPERTRADE;////
        }
      }else{
        //TTPrint("no action\n");
      }
    }
    /*TTPrint("spread0,%s,%s,%f,%f,%f,%f,%f,%f,%f\n",
      mc->symbol.c_str(),sc->symbol.c_str(),lspread,sspread,
      band->s_threshold,band->liq_upper,band->liq_lower,band->l_threshold,
      spreadsum);
    TTPrint("spread1,%s,%s,%f,%f,%f,%f,%f,%f,%f\n",
      mc->symbol.c_str(),sc->symbol.c_str(),lspread,sspread,
      band->s_threshold+spreadsum,band->liq_upper,band->liq_lower,band->l_threshold-spreadsum,
      spreadsum);*/
    if (abs(lspread-sspread) < 1.5){// do not log when spread > $1
      TTPrint("spread1,%s,%s,%f,%f,%f,%f,%f,%f,%f\n",
        mc->symbol.c_str(),sc->symbol.c_str(),lspread,sspread,
        band->s_threshold+spreadsum,band->liq_upper,band->liq_lower,band->l_threshold-spreadsum,
        (lspread-sspread));
    } 
  }
}
